<template>
  <section class="page">
    <nav class="left">
      <SliedrMenu @change="handler" />
    </nav>
    <main>
      <header v-if="urls?.list?.length">
        <div class="links">
          <div @click="urls.index = i" class="link-item" v-for="(u, i) in urls.list"
            :class="urls.index === i ? 'active' : ''">
            <div>
              <span>链接{{ i }}</span>
            </div>
            <el-icon @click.stop="delUrl(i)">
              <Close />
            </el-icon>
          </div>
          <div class="add-item" @click="handler('document')">
            <div>
              <el-icon>
                <CirclePlusFilled />
              </el-icon>
            </div>
          </div>
        </div>
        <div></div>
      </header>
      <webview :src="urls.list[urls.index]" class="iframe-style" allowpopups disablewebsecurity></webview>
      <!-- <iframe
        :src="urls.list[urls.index]"
        class="iframe-style"
        allowpopups
        allowfullscreen="true"
      ></iframe> -->
    </main>
    <UrlDialog v-model:visible="visible" @change="createNewView" />
    <chart ref="chart" v-model:visible="chartData.visible"></chart>
  </section>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import Chart from './chart.vue';
import SliedrMenu from './menu.vue';
import UrlDialog from './UrlDialog.vue';
import { Close, CirclePlusFilled } from '@element-plus/icons-vue';

const visible = ref(false);
const chartData = ref({ visible: false, list: [] });
const handler = (key: any) => {
  if (key === 'chat') {
    chartData.value.visible = true;
  } else if (key === 'document') {
    visible.value = true;
    chartData.value.visible = false;
  }
};
type Urls = {
  list: string[];
  index: number;
};
const urls = ref<Urls>({
  list: [],
  index: 0
});
const delUrl = (index: number) => {
  urls.value.list.splice(index, 1);
};
const createNewView = (url: string) => {
  urls.value.list.push(url);
  urls.value.index = urls.value.list.length - 1;
  visible.value = false;
};
onMounted(() => {
  const webview = document.querySelector('webview')
  webview?.addEventListener('dom-ready', () => {
    webview?.openDevTools()
  })
});
</script>
<style lang="less" scoped>
.page {
  display: grid;
  width: 100vw;
  height: 100vh;
  user-select: none;
  overflow: hidden;
  grid-template-rows: 0 1fr;
  grid-template-columns: 60px 1fr;
  grid-template-areas: 'left main' 'left main';
}

.links {
  display: flex;
  gap: 10px;
  align-items: center;
  height: 100%;
  padding: 0 10px;

  .add-item {
    cursor: pointer;
    color: var(--el-color-info);
    opacity: 0.3;
    transition: 300ms;
    padding-top: 6px;
    font-size: 22px;

    &:hover {
      opacity: 0.5;
    }
  }

  .link-item {
    background-color: var(--el-color-info-light-9);
    border-radius: var(--el-border-radius-base);
    color: var(--el-color-info);
    cursor: pointer;
    font-size: var(--el-font-size-base);
    font-weight: bold;
    line-height: var(--el-font-size-base);
    padding: 7px 15px;
    transition: var(--el-transition-all);
    position: relative;
    transition: 300ms;

    span {
      transition: 300ms;
      position: relative;
      transform: translateX(0%);
    }

    i {
      opacity: 0;
      transition: 300ms;
      position: absolute;
      transform: translateX(50%);
      top: 6px;
      right: 3px;
    }

    &:hover {
      background-color: var(--el-color-info-light-8);

      i {
        opacity: 1;
        transform: translateX(0%);
      }

      span {
        transform: translateX(-10%);
      }
    }

    &.active {
      background-color: var(--el-color-primary-light-8);
      color: var(--el-color-primary);
    }
  }
}

header {
  height: 40px;
  border-bottom: 1px solid var(--border-color);
}

nav {
  position: relative;
  transition: all 0.3s ease-in-out;
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  overflow-x: hidden;

  &.left {
    grid-area: left;
  }

  &.menu {
    grid-area: menu;
  }

  &.tree {
    grid-area: tree;
  }

  .nav-bar {
    position: absolute;
    top: 0;
    width: 11px;
    right: -6px;
    height: 100%;
    cursor: ew-resize;
  }
}

main {
  grid-area: main;
}

footer {
  grid-column: foot;
  border-top: 1px solid var(--border-color);
}
</style>
